package com.SouthernWall_404.AgeDust.Common.API.Util;

import com.SouthernWall_404.AgeDust.Common.API.Enum.raceEnum;
import com.SouthernWall_404.AgeDust.Common.API.Enum.sexEnum;
import com.SouthernWall_404.AgeDust.Common.API.Info.AttributesInfo;
import com.SouthernWall_404.AgeDust.Common.API.Info.CharacterInfo;
import net.minecraft.nbt.CompoundTag;
import org.antlr.runtime.misc.IntArray;

import java.util.Arrays;

public class NBTHandler {

    /*
    用于建立新的modPack
     */
    public static CompoundTag createModPack(CharacterInfo characterInfo, AttributesInfo attributesInfo)
    {
        CompoundTag modPack=new CompoundTag();
        modPack.put("character", createCharacterPack(
                characterInfo.name,
                characterInfo.sex,
                characterInfo.race,
                characterInfo.age
        ));

        modPack.put("attribute",createAttributePack(attributesInfo));
        return modPack;
    }

    public static CompoundTag createAttributePack(AttributesInfo attributesInfo)
    {
        CompoundTag attributePack=new CompoundTag();
        attributePack.putIntArray("level",attributesInfo.levelArray);
        attributePack.putIntArray("xp",attributesInfo.xpArray);

        return attributePack;
    }
    /*
    用于建立新的modPack.character
     */
    public static CompoundTag createCharacterPack(String name, sexEnum sex, raceEnum race, int age)
    {
        CompoundTag characterPack=new CompoundTag();
        characterPack.putString("name",name);
        characterPack.putInt("age",age);
        characterPack.putString("sex",sex.name());
        characterPack.putString("race",race.name());

        return characterPack;
    }
    /*
    用于从modPack中获取modPack.character
     */
    public static CharacterInfo loadCharacterFromModPack(CompoundTag modPack)
    {
        CompoundTag characterPack=modPack.getCompound("character");
        CharacterInfo characterInfo =new CharacterInfo();
        if(characterPack!=null)
        {
            characterInfo =loadCharacterPack(characterPack);
        }

        return characterInfo;
    }
    /*
用于从modPack.character获取玩家信息
 */
    public static CharacterInfo loadCharacterPack(CompoundTag characterPack)
    {
        CharacterInfo characterInfo =new CharacterInfo();

        characterInfo.age=characterPack.getInt("age");
        characterInfo.race=raceEnum.valueOf(characterPack.getString("race"));
        characterInfo.sex=sexEnum.valueOf(characterPack.getString("sex"));
        characterInfo.name=characterPack.getString("name");

        return characterInfo;
    }
    public static AttributesInfo loadAttributeFromModPack(CompoundTag modPack)
    {
        CompoundTag attributePack=modPack.getCompound("attribute");
        AttributesInfo attributesInfo =new AttributesInfo();
        if(attributePack!=null)
        {
            attributesInfo =loadAttributePack(attributePack);
        }

        return attributesInfo;
    }

    public static AttributesInfo loadAttributePack(CompoundTag attributePack)
    {

        AttributesInfo attributesInfo=new AttributesInfo();

        if(Arrays.stream(attributePack.getIntArray("level")).sum()!=0)
        {
            attributesInfo.levelArray=attributePack.getIntArray("level");
        }

        if(Arrays.stream(attributePack.getIntArray("xp")).sum()!=0) {
            attributesInfo.xpArray=attributePack.getIntArray("xp");
        }


        return attributesInfo;
    }



}
